<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Add a gradient sky layer to a map</title>
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <script src="../../lib/mapbox-gl.js"></script>
    <link href="../../lib/mapbox-gl.css" rel="stylesheet" />
    <script src="../../lib/axios.min.js"></script>
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      let baseDataUrl = `http://${localStorage.getItem(
        "host"
      )}/mapbox-gl-js-offline-examples`;
      axios
        .get(
          baseDataUrl +
            `/resources/styles/${localStorage.getItem(
              "mbtilesStylesName"
            )}.json`
        )
        .then((res) => {
          let data = res.data;
          initMap(data);
        })
        .catch((err) => {
          console.error(err);
        });

      function initMap(style) {
        let map = new mapboxgl.Map({
          container: "map",
          center: [116.392, 39.913],
          zoom: 13.5,
          bearing: -2.5,
          pitch: 83,
          style: style,
        });

        map.on("load", function () {
          map.addLayer({
            id: "sky",
            type: "sky",
            paint: {
              // set up the sky layer to use a color gradient
              "sky-type": "gradient",
              // the sky will be lightest in the center and get darker moving radially outward
              // this simulates the look of the sun just below the horizon
              "sky-gradient": [
                "interpolate",
                ["linear"],
                ["sky-radial-progress"],
                0.8,
                "rgba(135, 206, 235, 1.0)",
                1,
                "rgba(0,0,0,0.1)",
              ],
              "sky-gradient-center": [0, 0],
              "sky-gradient-radius": 90,
              "sky-opacity": [
                "interpolate",
                ["exponential", 0.1],
                ["zoom"],
                5,
                0,
                22,
                1,
              ],
            },
          });
        });
      }
    </script>
  </body>
</html>
